import pandas as pd
import numpy as np
from datetime import datetime
import random
s1= pd.Series([1,3,5,7,9],index=['A','B','C','D','E'],name='序号')
print(s1.replace(1,10))#下标为1的值改为10
#设置小数位数
date_list = [
    datetime(2016,9,10),
    datetime(2016,9,1),
    datetime(2016,10,10),
    datetime(2016,9,20),
    datetime(2017,9,10),
    datetime(2016, 7, 20),
    datetime(2017, 5, 10),


]
df = pd.DataFrame({
    'A':[0.123,0.285,0.452],
    'B':[0.183,0.245,0.492],
    'C':[0.264,0.286,0.452],
})
print(df.round(2))#对所有元素起作用 round中的参数是整数
print(df.round({'A':1,'B':2}))#对指定列保留小数，A列保留1位，B列保留2位round中的参数是字典
df2 = df.applymap(lambda x:'{:.2f}'.format(x))#使用自定义函数实现保留小数位置
print(df2)
df3 = pd.DataFrame({
    'A':[0.153,0.285,0.454],
    'B':[0.563,0.245,0.472],
    'C':[0.674,0.286,0.412],
})
df3['百分比'] = df3['A'].apply(lambda x:format(x,'.0%'))#对A列设定百分比保留整数
print(df3)
df3['百分比'] = df3['A'].apply(lambda x:format(x,'.2%'))#对A列设定百分比保留2位数
print(df3)
df3['百分比'] = df3['A'].map(lambda x:format(x,'.2%'))#对A列设定百分比保留2位数
print(df3)

#设置千位分隔符号 一般所有计算完毕后再改为千位分隔符

df4 = pd.DataFrame({
    'A':[10000,28000,45000],
    'B':[15000,28800,44000],
    'C':[10030,27000,45060],
})
df4['A'] = df4['A'].apply(lambda x:format(int(x),','))
print(df4)
print(df4.info())#查看类型
#行相加&列相加
df5 = pd.DataFrame({
    'A':[10000,28000,45000],
    'B':[15000,28800,44000],
    'C':[10030,27000,45060],
})
df5_rows_sum = df5.apply(lambda x:x.sum(),axis=0)
print(f'行求和{df5_rows_sum}')
df5_columns_sum = df5.apply(lambda x:x.sum(),axis=1)
print(f'列求和{df5_columns_sum}')